package com.comprehensive.dataservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comprehensive.common.dto.PageResponse;
import com.comprehensive.dataservice.common.dtoConverter.PageConverter;
import com.comprehensive.dataservice.entity.Dictionary;
import com.comprehensive.dataservice.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典项管理控制器
 * 提供字典项的CRUD操作和多条件查询功能
 */
@RestController
@RequestMapping("/dictionary")

public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 获取所有字典项
     * @return 字典项列表
     */
    @GetMapping
    public List<Dictionary> getAllDictionaries() {
        return dictionaryService.list();
    }

    /**
     * 根据ID获取字典项
     * @param id 字典项ID
     * @return 字典项详情
     */
    @GetMapping("/{id}")
    public Dictionary getDictionaryById(@PathVariable Long id) {
        return dictionaryService.getById(id);
    }

    /**
     * 创建新字典项
     * @param dictionary 字典项信息
     * @return 是否创建成功
     */
    @PostMapping
    public boolean createDictionary(@RequestBody Dictionary dictionary) {
        return dictionaryService.save(dictionary);
    }

    /**
     * 更新字典项
     * @param dictionary 新的字典项信息
     * @return 是否更新成功
     */
    @PutMapping
    public boolean updateDictionary(@RequestBody Dictionary dictionary) {
        return dictionaryService.updateById(dictionary);
    }

    /**
     * 删除字典项
     * @param id 字典项ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    
    public boolean deleteDictionary(@PathVariable Long id) {
        return dictionaryService.removeById(id);
    }

    /**
     * 多条件查询字典项
     * @param dictType 字典类型
     * @param dictCode 字典编码
     * @param status 状态(0-禁用,1-启用)
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页查询结果
     */
    @GetMapping("/search")
    
    public PageResponse<Dictionary> searchDictionaries(
            @RequestParam(required = false) String dictType,
            @RequestParam(required = false) String dictCode,
            @RequestParam(required = false) Integer enabled,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<Dictionary> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasText(dictType)) {
            queryWrapper.eq("dict_type", dictType);
        }
        if (StringUtils.hasText(dictCode)) {
            queryWrapper.eq("dict_code", dictCode);
        }
        if (enabled != null) {
            queryWrapper.eq("enabled", enabled);
        }

        return PageConverter.convert(dictionaryService.page(page, queryWrapper));
    }
    /**
     * 根据字典类型或字典编码查询字典项
     * @param dictType 字典类型
     * @param dictCode 字典编码
     * @return 字典项列表
     */
    @GetMapping("/queryDictionariesByTypeOrCode")
    public List<Dictionary> queryDictionariesByTypeOrCode(@RequestParam(required = false) String dictType, @RequestParam(required = false) String dictCode) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enabled", 1);
        if (StringUtils.hasText(dictType)) {
            queryWrapper.eq("dict_type", dictType);
        }
        if (StringUtils.hasText(dictCode)) {
            queryWrapper.eq("dict_code", dictCode);
        }
        return dictionaryService.list(queryWrapper);
    }
}